package com.lhkj.ct.meta.modules.patient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lhkj.ct.base.constants.SystemConstants;
import com.lhkj.ct.base.enums.DeleteStatus;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.mybatis.extension.service.impl.IServiceImpl;
import com.lhkj.ct.base.utils.Asserts;
import com.lhkj.ct.base.utils.OptionUtil;
import com.lhkj.ct.base.utils.TreeUtil;
import com.lhkj.ct.meta.modules.patient.mapper.PatUnitMapper;
import com.lhkj.ct.meta.modules.patient.model.entity.TblPatUnit;
import com.lhkj.ct.meta.modules.patient.service.PatUnitService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 单位信息表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-09-27
 */
@Service
public class PatUnitServiceImpl extends IServiceImpl<PatUnitMapper, TblPatUnit> implements PatUnitService {

    @Override
    public List<TblPatUnit> selectPatUnitTree(TblPatUnit patUnit) {
        List<TblPatUnit> patUnits = getBaseMapper().selectPatUnitTree(patUnit);
        return TreeUtil.builderTree(patUnits);
    }

    @Override
    public List<Option<String>> listOptions(TblPatUnit patUnit, PageQuery pageQuery) {
        LambdaUpdateWrapper<TblPatUnit> queryWrapper = new LambdaUpdateWrapper<TblPatUnit>()
                .like(StringUtils.isNotBlank(patUnit.getUnName()), TblPatUnit::getUnName, patUnit.getUnName())
//                .eq(StringUtils.isNotBlank(patUnit.getOrganId()), TblPatUnit::getOrganId, patUnit.getOrganId())
                .orderByAsc(TblPatUnit::getSort);
        List<TblPatUnit> patUnitList = getBaseMapper().selectList(queryWrapper);
        return OptionUtil.buildTreeOption(patUnitList, TblPatUnit::getUnName);
    }

    @Override
    public int insert(TblPatUnit entity) {
        generateTreePath(entity);
        return getBaseMapper().insert(entity);
    }


    @Override
    public int updateById(TblPatUnit entity) {
        generateTreePath(entity);
        return getBaseMapper().updateById(entity);
    }

    @Override
    public int removeByIds(Collection<? extends Serializable> idList) {
        for (Serializable unitId : idList) {
            Asserts.assertFalse(hasChildByUnitId(unitId), "存在下级单位,不允许删除");
            Asserts.assertFalse(checkUnitExistUser(unitId), "单位存在用户,不允许删除");
        }
        return getBaseMapper().deleteBatchIds(idList);
    }

    private void generateTreePath(TblPatUnit unit) {
        if (SystemConstants.ROOT_NODE_ID.equals(unit.getParentId())) {
            unit.setLevel(1);
        } else {
            TblPatUnit parent = this.getById(unit.getParentId());
            Asserts.assertFalse(null == parent, "上级单位不存在");
            unit.setTreePath(parent.getTreePath() + "," + parent.getId());
            unit.setLevel(parent.getLevel() + 1);
        }
    }

    public boolean checkUnitExistUser(Serializable unitId) {
        return getBaseMapper().checkUnitExistUser((String) unitId) > 0;
    }

    public boolean hasChildByUnitId(Serializable deptId) {
        Long count = this.getBaseMapper().selectCount(new LambdaQueryWrapper<TblPatUnit>()
                .select(TblPatUnit::getId)
                .eq(TblPatUnit::getParentId, deptId)
                .eq(TblPatUnit::getDelFlag, DeleteStatus.NORMAL));
        return count > 0;
    }

}
